home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / GR / GR_TEST.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  14KB  |  496 lines

  1. /****************************************************************
  2.  
  3.     gr_test.c       Validation program for Bywater
  4.             Graphics Interface standard
  5.  
  6.             Copyright (c) 1991, Ted A. Campbell
  7.  
  8.             Bywater Software
  9.             P. O. Box 4023 
  10.             Duke Station 
  11.             Durham, NC  27706
  12.  
  13.             email: tcamp@hercules.acpub.duke.edu
  14.  
  15.     Copyright and Permissions Information:
  16.  
  17.     All U.S. and international copyrights are claimed by the
  18.     author. The author grants permission to use this code
  19.     and software based on it under the following conditions:
  20.     (a) in general, the code and software based upon it may be 
  21.     used by individuals and by non-profit organizations; (b) it
  22.     may also be utilized by governmental agencies in any country,
  23.     with the exception of military agencies; (c) the code and/or
  24.     software based upon it may not be sold for a profit without
  25.     an explicit and specific permission from the author, except
  26.     that a minimal fee may be charged for media on which it is
  27.     copied, and for copying and handling; (d) the code must be 
  28.     distributed in the form in which it has been released by the
  29.     author; and (e) the code and software based upon it may not 
  30.     be used for illegal activities. 
  31.  
  32. ****************************************************************/
  33.  
  34. #include "stdio.h"
  35. #include "signal.h"
  36.  
  37. #ifdef __STDC__
  38. #include "malloc.h"
  39. #else
  40. extern char * malloc();
  41. #define size_t  int
  42. #endif
  43.  
  44. #include "bw.h"
  45. #include "gr.h"
  46. #include "kb.h"
  47.  
  48. struct gr_window main_window;
  49. char tbuf[ 128 ];
  50. char bw_ebuf[ BW_EBUFSIZE ];
  51. int x_pos, y_pos, b_stat;
  52. int x, y;
  53. int bsize;
  54. static int image;
  55. int test_quit();
  56.  
  57. main()
  58.    {
  59.    register int c;
  60.  
  61.    gr_init( &main_window, NULL );
  62.    kb_init();
  63.  
  64.    signal( SIGTERM, test_quit );
  65.  
  66.    /* set a font equal to 1/25 of the screen height */
  67.  
  68.    gr_font( GR_PRIMARY, F_DEFAULT, main_window.ymax / 25 );
  69.  
  70.    c = 0;
  71.    while ( c != 0x1b )
  72.       {
  73.       gr_cls( GR_PRIMARY );
  74.       gr_rectangle( GR_PRIMARY, 0, txt_y( 0 ), main_window.xmax, main_window.ymax,
  75.      WHITE, SOLID );
  76.       gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 0 ),
  77.      " Bywater gr (Graphics and Mouse) Interface Validation: ",
  78.      BLACK, WHITE );
  79.       if ( gr_ismouse == TRUE )
  80.      {
  81.      gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 3 ),
  82.         "Mouse detected.", WHITE, BLACK );
  83.      }
  84.       else
  85.      {
  86.      gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 3 ),
  87.         "No mouse detected.", WHITE, BLACK );
  88.      }
  89.       sprintf( tbuf, "Screens supported:  %d ", gr_screens );
  90.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 4 ), tbuf, WHITE, BLACK );
  91.       sprintf( tbuf, "Window size:  X axis, %d pixels; Y axis, %d pixels",
  92.      main_window.xmax, main_window.ymax );
  93.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 5 ), tbuf, WHITE, BLACK );
  94.  
  95.       sprintf( tbuf, "Pixel size:  X axis, %d pixels; Y axis, %d pixels",
  96.      gr_pxsize, gr_pysize );
  97.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 6 ), tbuf, WHITE, BLACK );
  98.  
  99.       sprintf( tbuf, "Font size:  X axis, %d pixels; Y axis, %d pixels",
  100.      main_window.fxsize, main_window.fysize );
  101.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 7 ), tbuf, WHITE, BLACK );
  102.  
  103.       sprintf( tbuf, "Colors available: %d", gr_colors );
  104.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 8 ), tbuf, WHITE, BLACK );
  105.  
  106.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 10 ),
  107.      " 1  Validate gr_font() and gr_text()", WHITE, BLACK );
  108.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 11 ),
  109.      " 2  Validate gr_pixel()", WHITE, BLACK );
  110.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 12 ),
  111.      " 3  Validate gr_line()", WHITE, BLACK );
  112.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 13 ),
  113.      " 4  Validate gr_rectangle()", WHITE, BLACK );
  114.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 14 ),
  115.      " 5  Validate gr_circle()", WHITE, BLACK );
  116.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 15 ),
  117.      " 6  Validate gr_imsave()", WHITE, BLACK );
  118.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 16 ),
  119.      " 7  Validate gr_blit()", WHITE, BLACK );
  120.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 17 ),
  121.      " 8  Validate gr_mouse()", WHITE, BLACK );
  122.       gr_text( GR_PRIMARY, txt_x( 5 ), txt_y( 19 ),
  123.      " 9  EXIT", WHITE, BLACK );
  124.  
  125.       c = kb_rx();
  126.  
  127.       switch( c )
  128.      {
  129.      case '1':
  130.         val_font();
  131.         break;
  132.      case '2':
  133.         val_pixel();
  134.         break;
  135.      case '3':
  136.         val_line();
  137.         break;
  138.      case '4':
  139.         val_rectangle();
  140.         break;
  141.      case '5':
  142.         val_circle();
  143.         break;
  144.      case '6':
  145.         val_save();
  146.         break;
  147.      case '7':
  148.         val_blit();
  149.         break;
  150.      case '8':
  151.         val_mouse();
  152.         break;
  153.      case '9':
  154.      case 0x1b:
  155.         c = 0x1b;
  156.         break;
  157.      }
  158.  
  159.  
  160.       }
  161.  
  162.    val_exit();
  163.  
  164.    }
  165.  
  166.  
  167. /* 1: validate gr_font(), and gr_text()   */
  168.  
  169. val_font()
  170.    {
  171.    gr_cls( GR_PRIMARY );
  172.    gr_rectangle( GR_PRIMARY, 0, txt_y( 0 ), main_window.xmax, main_window.ymax,
  173.       WHITE, SOLID );
  174.    gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 0 ),
  175.       "GR test # 1:  gr_font() validation.",
  176.       BLACK, WHITE );
  177.  
  178.    x = main_window.xmax / 10;
  179.    y = main_window.ymax / 18;
  180.  
  181.    gr_font( GR_PRIMARY, F_DEFAULT, main_window.ymax / 40 );
  182.    sprintf( tbuf, "Test size ymax / 40:  %dx%d font",
  183.       main_window.fysize, main_window.fxsize );
  184.    gr_text( GR_PRIMARY, x, y, tbuf, WHITE, BLACK );
  185.  
  186.    gr_font( GR_PRIMARY, F_DEFAULT, main_window.ymax / 30 );
  187.    sprintf( tbuf, "Test size ymax / 30:  %dx%d font",
  188.       main_window.fysize, main_window.fxsize );
  189.    gr_text( GR_PRIMARY, x, y * 2, tbuf, WHITE, BLACK );
  190.  
  191.    gr_font( GR_PRIMARY, F_DEFAULT, main_window.ymax / 25 );
  192.    sprintf( tbuf, "Test size ymax / 25:  %dx%d font",
  193.       main_window.fysize, main_window.fxsize );
  194.    gr_text( GR_PRIMARY, x, y * 4, tbuf, WHITE, BLACK );
  195.  
  196.    gr_font( GR_PRIMARY, F_DEFAULT, main_window.ymax / 20 );
  197.    sprintf( tbuf, "Test size ymax / 20:  %dx%d font",
  198.       main_window.fysize, main_window.fxsize );
  199.    gr_text( GR_PRIMARY, x, y * 8, tbuf, WHITE, BLACK );
  200.  
  201.    gr_font( GR_PRIMARY, F_DEFAULT, main_window.ymax / 15 );
  202.    sprintf( tbuf, "Test size ymax / 15:  %dx%d font",
  203.       main_window.fysize, main_window.fxsize );
  204.    gr_text( GR_PRIMARY, x, y * 12, tbuf, WHITE, BLACK );
  205.  
  206.    gr_font( GR_PRIMARY, F_DEFAULT, main_window.ymax / 25 );
  207.    kb_rx();
  208.  
  209.    }
  210.  
  211. /* 2: validate gr_pixel() */
  212.  
  213. val_pixel()
  214.    {
  215.    register int c;
  216.  
  217.    gr_cls( GR_PRIMARY );
  218.    gr_rectangle( GR_PRIMARY, 0, txt_y( 0 ), main_window.xmax, main_window.ymax,
  219.       WHITE, SOLID );
  220.    gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 0 ),
  221.       "GR test # 2:  gr_pixel() validation.",
  222.       BLACK, WHITE );
  223.  
  224.    x = main_window.xmax / 4;
  225.    y = ( main_window.ymax / 4 ) * 3;
  226.    c = 0;
  227.    while ( ( c < gr_colors ) && ( y > main_window.fysize ) )
  228.       {
  229.       gr_pixel( GR_PRIMARY, x, y, c );
  230.       sprintf( tbuf, " <- pixel color %d at x = %d, y = %d", 
  231.          c, x, y );
  232.       gr_text( GR_PRIMARY, x + ( 3 * main_window.fxsize ), 
  233.          y - ( main_window.fysize / 2 ), tbuf, WHITE, BLACK );
  234.       ++c;
  235.       y -= ( main_window.fysize / 2 ) * 3;
  236.       }
  237.    kb_rx();
  238.  
  239.    }
  240.  
  241. /* 3: validate gr_line() */
  242.  
  243. val_line()
  244.    {
  245.    register int c;
  246.  
  247.    gr_cls( GR_PRIMARY );
  248.    gr_rectangle( GR_PRIMARY, 0, txt_y( 0 ), main_window.xmax, main_window.ymax,
  249.       WHITE, SOLID );
  250.    gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 0 ),
  251.       "GR test # 3:  gr_line() validation.",
  252.       BLACK, WHITE );
  253.  
  254.    x = main_window.xmax / 8;
  255.    y = ( main_window.ymax / 4 ) * 3;
  256.    c = 0;
  257.    while ( ( c < gr_colors ) && ( y > main_window.fysize ) )
  258.       {
  259.       gr_line( GR_PRIMARY, x, y, x + main_window.xmax / 20, y, 
  260.          c, SOLID );
  261.       x += main_window.xmax / 10;
  262.       gr_line( GR_PRIMARY, x, y, x + main_window.xmax / 20, y, 
  263.          c, HATCH );
  264.       sprintf( tbuf, " <- lines, color %d at x = %d, y = %d", 
  265.          c, x, y );
  266.       gr_text( GR_PRIMARY, x + ( 3 * ( main_window.xmax / 20 )), 
  267.          y - ( main_window.fysize / 2 ), tbuf, WHITE, BLACK );
  268.       ++c;
  269.       x -= main_window.xmax / 10;
  270.       y -= ( main_window.fysize / 2 ) * 3;
  271.       }
  272.    kb_rx();
  273.  
  274.    gr_cls( GR_PRIMARY );
  275.    for ( x = 0; x < main_window.xmax; x += 15 )
  276.       {
  277.       gr_line( GR_PRIMARY, x, 0, 0, main_window.ymax,
  278.      WHITE, SOLID );
  279.       }
  280.  
  281.    for ( y = main_window.ymax - 10; y > 0; y -= 15 )
  282.       {
  283.       gr_line( GR_PRIMARY, 0, y, main_window.xmax, 0,
  284.      WHITE, SOLID );
  285.       }
  286.  
  287.    kb_rx();
  288.  
  289.    }
  290.  
  291. /* 4: validate gr_rectangle() */
  292.  
  293. val_rectangle()
  294.    {
  295.  
  296.    gr_cls( GR_PRIMARY );
  297.    gr_rectangle( GR_PRIMARY, 0, txt_y( 0 ), main_window.xmax, main_window.ymax,
  298.       WHITE, SOLID );
  299.    gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 0 ),
  300.       "GR test # 4:  gr_rectangle() validation.",
  301.       BLACK, WHITE );
  302.  
  303.    x = main_window.xmax / 40;
  304.    y = main_window.ymax / 20;
  305.    gr_rectangle( GR_PRIMARY, x * 4, y * 16, x * 6, y * 18, WHITE, HOLLOW );
  306.    gr_text( GR_PRIMARY, x * 10, y * 17, "Hollow rectangle", WHITE, BLACK );
  307.    gr_rectangle( GR_PRIMARY, x * 4, y * 12, x * 6, y * 14, WHITE, SOLID );
  308.    gr_text( GR_PRIMARY, x * 10, y * 13, "Solid rectangle", WHITE, BLACK );
  309.     gr_rectangle( GR_PRIMARY, x * 4, y * 8, x * 6, y * 10, WHITE, GRID );
  310.    gr_text( GR_PRIMARY, x * 10, y * 9, "Grid rectangle", WHITE, BLACK );
  311.     gr_rectangle( GR_PRIMARY, x * 4, y * 4, x * 6, y * 6, WHITE, HATCH );
  312.    gr_text( GR_PRIMARY, x * 10, y * 5, "Hatch rectangle", WHITE, BLACK );
  313.    kb_rx();
  314.    }
  315.  
  316. /* 5: validate gr_circle() */
  317.  
  318. val_circle()
  319.    {
  320.    gr_cls( GR_PRIMARY );
  321.    gr_rectangle( GR_PRIMARY, 0, txt_y( 0 ), main_window.xmax, main_window.ymax,
  322.       WHITE, SOLID );
  323.    gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 0 ),
  324.       "GR test # 5:  gr_circle() validation.",
  325.       BLACK, WHITE );
  326.  
  327.    x = main_window.xmax / 40;
  328.    y = main_window.ymax / 20;
  329.    gr_circle( GR_PRIMARY, x * 5, y * 17, (y/2)*3, WHITE, HOLLOW );
  330.    gr_text( GR_PRIMARY, x * 10, y * 17, "Hollow circle", WHITE, BLACK );
  331.    gr_circle( GR_PRIMARY, x * 5, y * 13, (y/2)*3, WHITE, SOLID );
  332.    gr_text( GR_PRIMARY, x * 10, y * 13, "Solid circle", WHITE, BLACK );
  333.     gr_circle( GR_PRIMARY, x * 5, y * 9, (y/2)*3, WHITE, GRID );
  334.    gr_text( GR_PRIMARY, x * 10, y * 9, "Grid circle", WHITE, BLACK );
  335.     gr_circle( GR_PRIMARY, x * 5, y * 5, (y/2)*3, WHITE, HATCH );
  336.    gr_text( GR_PRIMARY, x * 10, y * 5, "Hatch circle", WHITE, BLACK );
  337.    kb_rx();
  338.    }
  339.  
  340. /* 6: test gr_imsave() */
  341.  
  342. val_save()
  343.  
  344.    {
  345.  
  346.    /* if saving is allowed, save a portion of the screen here */
  347.  
  348.    if ( gr_saving )
  349.       {
  350.       x = main_window.xmax / 10;
  351.       y = main_window.ymax / 18;
  352.  
  353.       gr_imsave( GR_PRIMARY, TRUE, x, y * 3, x * 6, y * 8, &image );
  354.        gr_rectangle( GR_PRIMARY, x, y * 3, x * 6, y * 8, WHITE, HOLLOW );
  355.  
  356.  
  357.  
  358.       /* now show the portion saved earlier */
  359.  
  360.       gr_cls( GR_PRIMARY );
  361.       gr_rectangle( GR_PRIMARY, 0, txt_y( 0 ), main_window.xmax, main_window.ymax,
  362.      WHITE, SOLID );
  363.       gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 0 ),
  364.      "GR test # 6:  gr_imsave() validation.",
  365.          BLACK, WHITE );
  366.  
  367.       gr_imsave( GR_PRIMARY, FALSE, x, y * 3, x * 6, y * 8, &image );
  368.            gr_rectangle( GR_PRIMARY, x, y * 3, x * 6, y * 8, WHITE, HOLLOW );
  369.       kb_rx();
  370.       }
  371.    }
  372.  
  373. /* 7: validate gr_blit() */
  374.  
  375. val_blit()
  376.    {
  377.  
  378.    /* if there is more than one screen, validate screen writes and blits */
  379.  
  380.    if ( gr_blitting == TRUE )
  381.       {
  382.       gr_cls( GR_PRIMARY );
  383.       gr_rectangle( GR_PRIMARY, 0, txt_y( 0 ), main_window.xmax, main_window.ymax,
  384.      WHITE, SOLID );
  385.       gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 0 ),
  386.      "GR test # 7:  gr_blit() validation.",
  387.      BLACK, WHITE );
  388.       gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 4 ),
  389.      "  this has been written to screen GR_PRIMARY.",
  390.      BLACK, WHITE );
  391.       gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 5 ),
  392.      "  but after you hit RETURN GR_HIDDEN ",
  393.      BLACK, WHITE );
  394.       gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 6 ),
  395.      "  will be blitted to this screen  ",
  396.      BLACK, WHITE );
  397.       gr_text( GR_HIDDEN, txt_x( 0 ), txt_y( 0 ),
  398.      "  gr_blit() validation.",
  399.      BLACK, WHITE );
  400.       gr_text( GR_HIDDEN, txt_x( 0 ), txt_y( 4 ),
  401.      "  this has been written to screen GR_HIDDEN.",
  402.      BLACK, WHITE );
  403.       gr_text( GR_HIDDEN, txt_x( 0 ), txt_y( 5 ),
  404.      "  and has now been blitted over ",
  405.      BLACK, WHITE );
  406.       gr_text( GR_HIDDEN, txt_x( 0 ), txt_y( 6 ),
  407.      "  to GR_PRIMARY  ",
  408.      BLACK, WHITE );
  409.  
  410.       kb_rx();
  411.       gr_blit( GR_HIDDEN, GR_PRIMARY, 0, 0,
  412.      main_window.xmax,
  413.      main_window.ymax );
  414.       kb_rx();
  415.       }
  416.    }
  417.  
  418. /* 8: validate mouse */
  419.  
  420. val_mouse()
  421.    {
  422.    register int c;
  423.  
  424.    /* If mouse exists, validate it */
  425.  
  426.    if ( gr_ismouse == TRUE )
  427.       {
  428.       gr_cls( GR_PRIMARY );
  429.       gr_rectangle( GR_PRIMARY, 0, txt_y( 0 ), main_window.xmax, main_window.ymax,
  430.      WHITE, SOLID );
  431.       gr_text( GR_PRIMARY, txt_x( 0 ), txt_y( 0 ),
  432.      "GR test # 8:  gr_mouse() validation.",
  433.          BLACK, WHITE );
  434.  
  435.       x = main_window.xmax / 40;
  436.       y = main_window.ymax / 20;
  437.  
  438.       while ( gr_mouse( STATUS, &x_pos, &y_pos, &b_stat ) != TRUE )
  439.      {
  440.      sprintf( tbuf, "Mouse position:  x = %d, y = %d    ",
  441.         x_pos, y_pos );
  442.      gr_text( GR_PRIMARY, x * 10, y * 17, tbuf, WHITE, BLACK );
  443.      }
  444.       gr_mouse( WAIT, &x_pos, &y_pos, &b_stat );
  445.       gr_mouse( WAIT, &x_pos, &y_pos, &b_stat );
  446.  
  447.       c = 0;
  448.       while( ( kb_rxstat() == FALSE ) && ( c < 12 ))
  449.          {
  450.      ++c;
  451.          gr_mouse( WAIT, &x_pos, &y_pos, &b_stat );
  452.          sprintf( tbuf, "Mouse position:  x = %d, y = %d    ",
  453.             x_pos, y_pos );
  454.          gr_text( GR_PRIMARY, x * 10, y * 17, tbuf, WHITE, BLACK );
  455.          }
  456.       }
  457.    }
  458.  
  459. val_exit()
  460.  
  461.    {
  462.  
  463.    /* Finished with validation:  clear screen and restore original state */
  464.  
  465.    gr_cls( GR_PRIMARY );
  466.    kb_deinit();
  467.    gr_deinit();
  468.    }
  469.  
  470. txt_y( line )
  471.    int line;
  472.    {
  473.    return ( main_window.ymax ) - ( main_window.fysize * ( line + 1 ));
  474.    }
  475.  
  476. txt_x( column )
  477.    int column;
  478.    {
  479.    return column * main_window.fxsize;
  480.    }
  481.  
  482. test_quit()
  483.    {
  484.    gr_cls( GR_PRIMARY );
  485.    kb_deinit();
  486.    gr_deinit();
  487.    }
  488.  
  489. bw_error( m )
  490.    char *m;
  491.    {
  492.    fprintf( stderr, "ERROR: %s \n", m );
  493.    }
  494.  
  495.  
  496.